/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"53CC85F3-C9A1-472B-9757-0B64C4BE0F41",variableType:4}
 */
var fSubCategoria = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"360314D6-1255-4F1C-A14D-470CD2208A19",variableType:4}
 */
var fCategoria = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"A39EA6B6-13E3-4B7E-81F9-C93A8AD02C7F",variableType:4}
 */
var fCaracteristica = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"D69B0C2F-8FED-441C-9883-34C131004861",variableType:4}
 */
var fValores = null;

/**
 * @param {String} newValue
 *
 * @properties={typeid:24,uuid:"CBADFD1F-175F-49E5-8946-CC21C5F8993C"}
 */
function parseValor(newValue) {
	/** @type {String} */
	var $nro = newValue
	if (newValue.toString().indexOf('\n',0) != -1)
	{	
		$nro = newValue.toString().substring(0,newValue.toString().indexOf('\n',0)+1)
	}
	/** @type {Number}*/
	var nro = parseInt($nro)
	return nro
}
/**
 * Handle changed data.
 *
 * @param {Number} oldValue old value
 * @param {Number} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"FD050191-C00B-4E68-A056-704F546EA12F"}
 */
function onDataChangeCategoria(oldValue, newValue, event) {
	var $nro = parseValor(newValue.toString())
	cargaValuelistSubc($nro)
	filtrar(newValue,null)
	fSubCategoria 		= null
	fCaracteristica 	= null
	fValores 			= null
	application.setValueListItems('valores_rel',new Array())
	application.setValueListItems('caracteristicas_rel',new Array())
	return true
}

/**
 * @param {Number} categoria
 *
 * @properties={typeid:24,uuid:"17E7209E-50C1-4959-A9DF-63F26E5DAAD7"}
 */
function cargaValuelistSubc(categoria)
{
	var query = 'SELECT sc.descripcion, c.prod_subcategoria_id FROM prod_categorias_x_prod_subcategorias as "c" INNER JOIN prod_subcategorias as "sc" ON c.prod_subcategoria_id = sc.prod_subcategoria_id WHERE c.prod_categoria_id = ?'
	var args = [categoria]
	var $ds = databaseManager.getDataSetByQuery('infocom',query,args,-1)
	
	application.setValueListItems('subcategorias_rel',$ds)
}

/**
 * @AllowToRunInFind
 * 
 * @param {Number} categoria
 * @param {Number} subcategoria
 *
 * @properties={typeid:24,uuid:"8FE5C4C6-9134-4BEF-A933-6D8D926F235A"}
 */
function filtrar(categoria, subcategoria)
{
	forms.productos_tbl.controller.find()
	
	forms.productos_tbl.prod_categoria_id 		= categoria
	forms.productos_tbl.prod_subcategoria_id 	= subcategoria
	
	forms.productos_tbl.controller.search()
}
/**
 * Handle changed data.
 *
 * @param {Number} oldValue old value
 * @param {Number} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"B07FEBB2-CE82-4101-BB8C-E9B5A6EBE70E"}
 */
function onDataChangeSubCategoria(oldValue, newValue, event) {
	var $nro = parseValor(newValue.toString())
	cargaValuelistCarac($nro)
	filtrar(fCategoria,newValue)
	fCaracteristica = null
	fValores 		= null
	application.setValueListItems('valores_rel',new Array())
	return true
}

/**
 * @param {Number} subcategoria
 *
 * @properties={typeid:24,uuid:"062D5724-D0F2-42B3-9822-6A10CD65C070"}
 */
function cargaValuelistCarac(subcategoria)
{
	var query = 'SELECT c.descripcion, sc.prod_subcat_x_carac_id FROM prod_subcat_x_carac as "sc" JOIN prod_caracteristicas as "c" ON c.prod_caracteristica_id = sc.prod_caracteristica_id WHERE sc.prod_subcategoria_id = ?'
	var args = [subcategoria]
	var $ds = databaseManager.getDataSetByQuery('infocom',query,args,-1)
	
	application.setValueListItems('caracteristicas_rel',$ds)
}
/**
 * Handle changed data.
 *
 * @param {Number} oldValue old value
 * @param {Number} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"A13F25E7-A5AE-41A1-9228-A172A5FF8924"}
 */
function onDataChangeCaracteristica(oldValue, newValue, event) {
	var $nro = parseValor(newValue.toString())
	cargaValuelistValores($nro)
//	filtrar(fCategoria,newValue)
	fValores = null
	return true
}

/**
 * @param {Number} caracteristica
 *
 * @properties={typeid:24,uuid:"2BFB568B-93A0-41CF-A813-F0720512405A"}
 */
function cargaValuelistValores(caracteristica)
{
	var query = 'SELECT v.valor, v.prod_subc_carac_valores_id FROM prod_subc_carac_valores as "v" WHERE v.prod_subc_x_carac_id = ? ORDER BY valor asc'
	var args = [caracteristica]
	var $ds = databaseManager.getDataSetByQuery('infocom',query,args,-1)
	
	application.setValueListItems('valores_rel',$ds)
}
/**
 * Handle changed data.
 *
 * @param {Number} oldValue old value
 * @param {Number} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"7B8EF7EE-E40C-41BB-853A-7BCC0B60476E"}
 * @AllowToRunInFind
 */
function onDataChangeValor(oldValue, newValue, event) {
	forms.productos_tbl.controller.find()
	
	forms.productos_tbl.prod_categoria_id 		= fCategoria
	forms.productos_tbl.prod_subcategoria_id 	= fSubCategoria
	forms.productos_tbl.productos_to_producto_x_subc_carac_valores.prod_subc_carac_valor_id = newValue
	
	forms.productos_tbl.controller.search()
	return true
}
